<template>
  <el-dialog v-model="dialogVisible" :title="title" width="500px" :append-to-body="true" :before-close="handleClose">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" status-icon>
      <el-form-item label="选择车辆" prop="plateNumber">
        <el-select v-model="ruleForm.plateNumber" placeholder="请选择" style="width: 100%" @change="handleChange">
          <el-option v-for="(item, index) in options" :key="index" :label="item.plateNumber" :value="item.plateNumber" />
        </el-select>
      </el-form-item>
       <el-form-item
        label="司机名称"
        prop="driverName"
       >
        <el-input
          placeholder="请输入"
          v-model="ruleForm.driverName" />
      </el-form-item>
       <el-form-item
        label="手机号"
        prop="driverPhone"
       >
        <el-input
          placeholder="请输入"
          v-model="ruleForm.driverPhone" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloses(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { REQUEST } from '@/api/request'
import { tmpdir } from 'os'
const ruleFormRef = ref<FormInstance>()
const title = ref('')
const dialogVisible = ref(false)
const emits = defineEmits(['ok'])
const options = ref([])
const ruleForm = ref({
  ids:[],
  plateNumber: '',
  driverName:'',
  driverPhone:''
})

const rules = ref({
  plateNumber: [{ required: true, message: '请选择', trigger: 'change' }],
  driverName: [{ required: true, message: '请输入', trigger: 'change' }],
  driverPhone: [{ required: true, message: '请输入', trigger: 'change' }]
})
const handleChange = (e: any) => {
  console.log(e)
  options.value.forEach((item: any) => {
    if (item.plateNumber == e) {
      ruleForm.value.driverName = item.nickName
      ruleForm.value.driverPhone = item.phone
    }
  })
}
const openModal = (t: any, e: any) => {
  ruleForm.value = {
    ids:[],
    plateNumber: '',
    driverName:'',
    driverPhone:''
  }
  ruleForm.value.ids = t.row
  dialogVisible.value = true
  title.value = t.title
  REQUEST.POST('CARLIST',{}).then((res: any) => {
    options.value = res.data
  })
}
const handleClose = () => {
  dialogVisible.value = false
  emits('ok')
}
const handleCloses = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emits('ok')
  dialogVisible.value = false
}
const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      REQUEST.POST('CARSETTING',ruleForm.value).then((res: any) => {
        if (res.status == 200) {
          ElMessage({
            type: 'success',
            message: '配置成功',
          })
          emits('ok')
        }
      })
      dialogVisible.value = false
    }
  })
}
defineExpose({ openModal })
</script>

<style lang="" scoped></style>
